package com.firebird.app.kotlin.stock.ui.godeal

import android.R
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.DetailsDealBean
import com.firebird.app.kotlin.stock.data.DetailsDealBean.PositionsBean
import com.firebird.app.kotlin.stock.ui.allocation_manage.Adp
import com.firebird.app.kotlin.stock.ui.nullpage.NullPersonActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils.closeDialog
import com.firebird.app.kotlin.stock.utils.DialogUtils.showDialog_progressbar
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils.Simple
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils.restLoginDialog
import com.firebird.app.kotlin.stock.utils.ToastUtil.showToast
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.data.EventMessage
import com.firebird.app.kotlin.stock.myview.CircularProgressView
import com.firebird.app.kotlin.stock.ui.buy.BuyActivity
import com.firebird.app.kotlin.stock.ui.detalsdeal.DetailsDealActivity
import com.firebird.app.kotlin.stock.ui.godeal.contract.GoDealContract
import com.firebird.app.kotlin.stock.ui.godeal.presenter.GoDealPresenter
import com.firebird.app.kotlin.stock.ui.godeal.tab.tab_haved.Adp
import com.firebird.app.kotlin.stock.ui.profit.ProfitActivity
import com.firebird.app.kotlin.stock.ui.recall.RecallActivity
import com.firebird.app.kotlin.stock.ui.sale.SaleActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil
import org.greenrobot.eventbus.EventBus


class GoDealActivity : BaseMvpActivity<GoDealPresenter?>(), GoDealContract.View {
    @BindView(R.id.bt_buy)
    var btBuy: LinearLayout? = null
    @BindView(R.id.bt_sale)
    var btSale: LinearLayout? = null
    @BindView(R.id.bt_recall)
    var btRecall: LinearLayout? = null
    @BindView(R.id.bt_deposit)
    var btDeposit: LinearLayout? = null
    @BindView(R.id.bt_dealdetals)
    var btDealdetals: LinearLayout? = null
    @BindView(R.id.back)
    var back: LinearLayout? = null
    @BindView(R.id.tv_orderid)
    var tvOrderid: TextView? = null
    @BindView(R.id.tv_money)
    var tvMoney: TextView? = null
    @BindView(R.id.tv_ketou)
    var tvKetou: TextView? = null
    @BindView(R.id.tv_ccsz)
    var tvCcsz: TextView? = null
    @BindView(R.id.tv_ccbl)
    var tvCcbl: TextView? = null
    @BindView(R.id.tv_fudong)
    var tvFudong: TextView? = null
    @BindView(R.id.tv_yujing)
    var tvYujing: TextView? = null
    @BindView(R.id.tv_pingcang)
    var tvPingcang: TextView? = null
    @BindView(R.id.progerss)
    var progerss: CircularProgressView? = null
    @BindView(R.id.iv_upordown)
    var ivUpordown: ImageView? = null
    @BindView(R.id.recycler)
    var recycler: RecyclerView? = null
    @BindView(R.id.ll_title)
    var llTitle: LinearLayout? = null
    @BindView(R.id.rl_nodata)
    var rlNodata: RelativeLayout? = null
    private var orderId = 0
    private var adp: Adp? = null
    private var dialog: Dialog? = null
    private val bundle = Bundle()
    override val layoutId: Int
        get() = R.layout.activity_go_deal

    override fun initView() {
        orderId = intent.extras.getInt("orderid")
        mPresenter = GoDealPresenter()
        mPresenter.attachView(this)
        keyList.add("bpOrderId")
        valueList.add(orderId.toString() + "")
        mPresenter.getData(Simple(keyList, valueList))
    }

    override fun showLoading() {
        dialog = showDialog_progressbar(this)
    }

    override fun hideLoading() {
        closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        closeDialog(dialog)
    }

    fun onSuccess(bean: DetailsDealBean) {
        if (bean.respCode == Config.SUCCESS) {
            tvMoney!!.text = bean.positionVal
            tvOrderid!!.text = "操盘方案号：" + bean.orderNo
            tvKetou!!.text = bean.useBalance
            tvCcsz!!.text = bean.totMarketVal
            tvCcbl!!.text = bean.positionRatio
            tvFudong!!.text = bean.totEarnAmt
            tvYujing!!.text = bean.warningAmt
            tvPingcang!!.text = bean.closeAmt
            if (bean.totEarnAmt!!.contains("-")) {
                tvFudong!!.setTextColor(Color.parseColor("#06b96a"))
                ivUpordown!!.setImageResource(R.mipmap.icon_down1)
            } else {
                tvFudong!!.setTextColor(Color.parseColor("#ff3025"))
                ivUpordown!!.setImageResource(R.mipmap.up)
            }
            val v1 = bean.totMarketVal!!.toDouble()
            val all = bean.positionVal!!.toDouble()
            val v2 = v1 / all
            val pro = (v2 * 100).toInt()
            progerss.setProgress(pro)
            //持仓数组
            val list: List<PositionsBean?>? = bean.positions
            if (list!!.size == 0) {
                llTitle!!.visibility = View.GONE
                rlNodata!!.visibility = View.VISIBLE
            } else {
                llTitle!!.visibility = View.VISIBLE
                rlNodata!!.visibility = View.GONE
                recycler.setLayoutManager(LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false))
                adp = Adp(R.layout.item_haved, list)
                recycler.setAdapter(adp)
                adp!!.openLoadAnimation()
            }
        } else if (bean.respCode == Config.TOKENOUT) {
            restLoginDialog(this)
        } else if (bean.respCode == Config.NONAME) {
            val message = EventMessage(1, "Deal")
            EventBus.getDefault().postSticky(message)
            toClass1(this, NullPersonActivity::class.java)
        } else {
            showToast(this, bean.respDesc)
        }
    }

    @OnClick(R.id.back, R.id.bt_buy, R.id.bt_sale, R.id.bt_recall, R.id.bt_deposit, R.id.bt_dealdetals)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.back -> finish()
            R.id.bt_buy -> {
                //买入
                bundle.putInt("orderid", orderId)
                toClass(this, BuyActivity::class.java, bundle)
            }
            R.id.bt_sale -> {
                //卖出
                bundle.putInt("orderid", orderId)
                toClass(this, SaleActivity::class.java, bundle)
            }
            R.id.bt_recall -> {
                //撤单
                bundle.putInt("orderid", orderId)
                toClass(this, RecallActivity::class.java, bundle)
            }
            R.id.bt_deposit -> {
                //提盈
                bundle.putInt("orderid", orderId)
                toClass(this, ProfitActivity::class.java, bundle)
            }
            R.id.bt_dealdetals -> {
                //交易明细
                bundle.putInt("orderid", orderId)
                toClass1(this, DetailsDealActivity::class.java, bundle)
            }
        }
    }
}